package com.itheima.stock.service.impl;

import com.itheima.stock.cache.Cache;
import com.itheima.stock.constant.Constants;
import com.itheima.stock.dto.LoginDTO;
import com.itheima.stock.entity.SysUser;
import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.Exceptions;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.service.UserService;
import com.itheima.stock.util.IdWorkers;
import com.itheima.stock.util.RedisKeyUtil;
import com.itheima.stock.vo.CaptchaVO;
import com.itheima.stock.vo.LoginVO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    private PasswordEncoder passwordEncoder;

    private final SysUserMapper sysUserMapper;

    private final IdWorkers idWorkers;

    private final Cache cache;

    private static final int FOUR = 4;

    public UserServiceImpl(PasswordEncoder passwordEncoder, Cache cache, IdWorkers idWorkers, SysUserMapper sysUserMapper) {
        this.passwordEncoder = passwordEncoder;
        this.cache = cache;
        this.idWorkers = idWorkers;
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public CaptchaVO captcha() {
        // 生成随机四位验证码
        String captcha = RandomStringUtils.randomNumeric(FOUR);
        // 将验证码保存到redis中(key设计 架构设计)
        // 生成redis key
        String captchaKey = RedisKeyUtil.generateKey(Constants.CAPTCHA_PREFIX, idWorkers.nextStrId());
        // 调用缓存接口 将验证码保存到redis中
        cache.set(captchaKey, captcha, Constants.CAPTCHA_EXPIRE_TIMEOUT);

        // 数组组装返回验证码
        return CaptchaVO.builder().code(captcha).rkey(captchaKey).build();

    }

    @Override
    public LoginVO login(LoginDTO loginDTO) {
        // 1.参数校验
        if (ObjectUtils.isEmpty(loginDTO)||
                ObjectUtils.isEmpty(loginDTO.getUsername())||
                ObjectUtils.isEmpty(loginDTO.getPassword())||
                ObjectUtils.isEmpty(loginDTO.getCode())||
                ObjectUtils.isEmpty(loginDTO.getRkey())){
            Exceptions.cast(CodeEnum.PARAM_IS_NOT_NULL);
        }

        // 2.核心登录逻辑
        // 1.校验验证码
        String captcha = cache.get(loginDTO.getRkey());
        if (!StringUtils.equals(captcha,loginDTO.getCode())){
            Exceptions.cast(CodeEnum.SYSTEM_VERIFY_CODE_ERROR);
        }
        //删除redis中的验证码
        cache.remove(loginDTO.getRkey());
        // 2.根据用户名查询用户信息
        SysUser user = sysUserMapper.selectByUsername(loginDTO.getUsername(),Constants.SYS_USER_STATUS_NORMAL,Constants.SYS_USER_UN_DELETED);

        // 3.判断用户是否存在 密码是否正确
        if (ObjectUtils.isEmpty(user) || !passwordEncoder.matches(loginDTO.getPassword(),user.getPassword())){
            Exceptions.cast(CodeEnum.SYSTEM_USERNAME_NOT_EXISTS);
        }

        // 3.数据组装返回
        return LoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .nickName(user.getNickName())
                .phone(user.getPhone())
                .build();
    }
}
